//
//  RegistrationPro.m
//  YZZX
//
//  Created by Easaa on 15/6/2.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "RegistrationPro.h"
#import "LoginViewController.h"

@interface RegistrationPro ()<HXHttpRequestDelegate,UIAlertViewDelegate,UITextFieldDelegate>
{
    
    HXHttpRequest  *_httpRequest;
    UITextField  *_userName;
    UITextField  *_passgeWord;
    UITextField  *_checkPassWord;

}

@end

@implementation RegistrationPro

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    self.title = @"注册";
    
    
    
    self.view.backgroundColor =RGBCOLOR(233, 233, 233);
    
    UIView  *LoginViewBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, Dev_ScreenWidth, 120)];
    LoginViewBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:LoginViewBgView];
    
    UIView  *LineViewBgView = [[UIView alloc]initWithFrame:CGRectMake(10, 39, Dev_ScreenWidth-10, 1)];
    LineViewBgView.backgroundColor = RGBCOLOR(233, 233, 233);
    [LoginViewBgView addSubview:LineViewBgView];
    
    UIView  *LineViewBgView0 = [[UIView alloc]initWithFrame:CGRectMake(10, 79, Dev_ScreenWidth-10, 1)];
    LineViewBgView0.backgroundColor = RGBCOLOR(233, 233, 233);
    [LoginViewBgView addSubview:LineViewBgView0];
    
    
    
    UILabel *labelTitleUser = [[UILabel alloc] initWithFrame:CGRectMake(10, 0,40, 40)];
    labelTitleUser.font = [UIFont boldSystemFontOfSize:15];         //UILabel的字体大小
    labelTitleUser.numberOfLines = 0;                               //必须定义这个属性，否则UILabel不会换行
    labelTitleUser.textAlignment = NSTextAlignmentLeft;             //文本对齐方式
    labelTitleUser.text =@"账号:";
    [LoginViewBgView addSubview:labelTitleUser];
    
    
    _userName =[[UITextField alloc]initWithFrame:CGRectMake(50, 0, Dev_ScreenWidth-60, 40)];
    _userName.tag =0;
    _userName.delegate =self;
    [_userName becomeFirstResponder];
    _userName.font = [UIFont systemFontOfSize:15];
    [LoginViewBgView addSubview:_userName];
    
    
    
    
    
    UILabel *labelTitleMassge = [[UILabel alloc] initWithFrame:CGRectMake(10, 40,40, 40)];
    labelTitleMassge.font = [UIFont boldSystemFontOfSize:15];           //UILabel的字体大小
    labelTitleMassge.numberOfLines = 0;                                 //必须定义这个属性，否则UILabel不会换行
    labelTitleMassge.textAlignment = NSTextAlignmentLeft;               //文本对齐方式
    //    labelTitleMassge.textColor = [UIColor grayColor];
    labelTitleMassge.text =@"密码:";
    
    [LoginViewBgView addSubview:labelTitleMassge];
    
    
    
    _passgeWord =[[UITextField alloc]initWithFrame:CGRectMake(50, 40, Dev_ScreenWidth-60, 40)];
    _passgeWord.tag =0;
     _passgeWord.delegate =self;
    _passgeWord.font = [UIFont systemFontOfSize:15];
    _passgeWord.secureTextEntry = YES;  //密文
    // PassWordText.backgroundColor = [UIColor greenColor];
    [LoginViewBgView addSubview:_passgeWord];
    
    
    
    
    
    
    UILabel *labeleMail = [[UILabel alloc] initWithFrame:CGRectMake(10, 80,70, 40)];
    labeleMail.font = [UIFont boldSystemFontOfSize:15];           //UILabel的字体大小
    labeleMail.numberOfLines = 0;                                 //必须定义这个属性，否则UILabel不会换行
    labeleMail.textAlignment = NSTextAlignmentLeft;               //文本对齐方式
    //    labelTitleMassge.textColor = [UIColor grayColor];
    labeleMail.text =@"确认密码:";
    [LoginViewBgView addSubview:labeleMail];
    
    
    
    _checkPassWord =[[UITextField alloc]initWithFrame:CGRectMake(80, 80, Dev_ScreenWidth-60, 40)];
    _checkPassWord.tag =0;
     _checkPassWord.delegate =self;
    _checkPassWord.font = [UIFont systemFontOfSize:15];
     _checkPassWord.secureTextEntry = YES;  //密文
    // PassWordText.backgroundColor = [UIColor greenColor];
    [LoginViewBgView addSubview:_checkPassWord];
    
    
    
    
    
    
    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.tag = 2014 ;
    bottomButton.frame =CGRectMake(10,LoginViewBgView.bottom+40 ,Dev_ScreenWidth-20, 40);
    [bottomButton addTarget:self action:@selector(RequestHeadlLinesAndHeadImageDataFantion) forControlEvents:UIControlEventTouchUpInside];
    [bottomButton.layer setMasksToBounds:YES];
    [bottomButton.layer setCornerRadius:2]; //设置矩圆角半径
    [bottomButton.layer setBorderWidth:0];   //边框宽度
    [bottomButton setTitle:@"注册" forState:UIControlStateNormal];
    [bottomButton setTintColor:[UIColor whiteColor]];
    bottomButton.backgroundColor =RGBCOLOR(252, 78, 125);
    [self.view addSubview:bottomButton];
    

    
    
}
- (void)RequestHeadlLinesAndHeadImageDataFantion
{
    if(_userName.text.length<=0||_passgeWord.text.length<=0||_checkPassWord.text.length<=0)
    {
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入完整！"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1024;
        [AlertView show];
        return;
        
    }else if (![_passgeWord.text isEqualToString:_checkPassWord.text])
    {
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"密码与确认密码不一致"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1025;
        [AlertView show];
        return;
    }
    
    
    
    
    NSDictionary* dic =  [NSDictionary dictionaryWithObjectsAndKeys:
                          _userName.text,        @"username",
                          _checkPassWord.text,   @"password",
                          self.eMail,       @"email",
                          self.nameXing,    @"last_name",
                          self.nameMing,    @"first_name",nil
                          ];
    
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               dic,                  @"post_data",
                               nil];
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_MEMBER_REGITSTER andTag:1001];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

}




#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
    NSDictionary *dic = [[NSDictionary alloc]init];
    dic = [JsonUtil jsonToDic:responseString];
    
    
    
    if(![dic[@"is_success"] boolValue])
    {
        
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:[dic objectForKey:@"message"]
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1028;
        [AlertView show];
        return;
    }else
    {
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"注册成功，去登录"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1029;
        [AlertView show];
    
    
    }
    
    if ([dic[@"data"] isEqual:[NSNull null]])
    {
        return;
    }
    NSString * strs = dic[@"data"];
    
    NSDictionary *dics =[JsonUtil jsonToDic:strs];
    
    NSLog(@"---------->%@",dics);
//    
//    //设置//用户名
//    NSUserDefaults *UserDefaultSize =[NSUserDefaults standardUserDefaults];
//    [UserDefaultSize setObject:[dics objectForKey:@"fullname"] forKey:YZZX_Login_fullname];
//    
//    //设置//用户id
//    
//    [UserDefaultSize setObject:[[dics objectForKey:@"user_id"] stringValue] forKey:YZZX_Login_user_id];
//    
//    //设置//站点id
//    
//    [UserDefaultSize setObject:[[dics objectForKey:@"site_id"] stringValue] forKey:YZZX_Login_site_id];
//    
//    
//    
//    //创建通知
//    NSNotification *notification =[NSNotification notificationWithName:YZZX_Login_Bool_Yes object:nil userInfo:nil];
//    //通过通知中心发送通知
//    [[NSNotificationCenter defaultCenter] postNotification:notification];
//    
    
   
    
    
    
}


#pragma mark  HXHttpRequest - 请求代理，，错误代理
- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    

}


#pragma mark alertView 弹框代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
     [self.view endEditing:YES];
    
    if(alertView.tag == 1029)
    {
        if(buttonIndex == 0)
        {
            NSUserDefaults *UserDefaultSize =[NSUserDefaults standardUserDefaults];
            [UserDefaultSize setObject:_userName.text forKey:YZZX_Login_name_zhuce];
            [UserDefaultSize setObject:_passgeWord.text forKey:YZZX_Login_password_zhuce];
            
            //创建通知
            NSNotification *notification =[NSNotification notificationWithName:YZZX_Login_zhuce_pop object:nil userInfo:nil];
            //通过通知中心发送通知
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            

            NSArray *arr = self.navigationController.viewControllers;
            for (UIViewController * Viewscot in arr)
            {
                if([Viewscot isKindOfClass:[LoginViewController class]])
                {
                
                [self.navigationController popToViewController:Viewscot animated:YES];
                
                }
                    

            }
            
            
            //[self.navigationController popToViewController:arr[1] animated:YES];
        }
    }if(alertView.tag == 1028)
    {
//      _userName.text = @"";
//      _passgeWord.text = @"";
//      _checkPassWord.text = @"";
    
    }
    
}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
     [theTextField resignFirstResponder];
 
    return YES;
}

@end
